home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_069 / make / macro.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  147 lines

  1. /*
  2.  *    Macro control for make
  3.  */
  4.  
  5.  
  6. #include "h.h"
  7.  
  8.  
  9. struct macro   *macrohead;
  10.  
  11.  
  12. struct macro   *
  13. getmp(name)
  14.     char           *name;
  15. {
  16.     register struct macro *rp;
  17.  
  18.     for (rp = macrohead; rp; rp = rp->m_next)
  19.     if (strcmp(name, rp->m_name) == 0)
  20.         return rp;
  21.     return (struct macro *) 0;
  22. }
  23.  
  24.  
  25. char           *
  26. getmacro(name)
  27.     char           *name;
  28. {
  29.     struct macro   *mp;
  30.  
  31.     if (mp = getmp(name))
  32.     return mp->m_val;
  33.     else
  34.     return "";
  35. }
  36.  
  37.  
  38. struct macro   *
  39. setmacro(name, val)
  40.     char           *name;
  41.     char           *val;
  42. {
  43.     register struct macro *rp;
  44.     register char  *cp;
  45.  
  46.  
  47.     /* Replace macro definition if it exists  */
  48.     for (rp = macrohead; rp; rp = rp->m_next)
  49.     if (strcmp(name, rp->m_name) == 0) {
  50.         free(rp->m_val);    /* Free space from old  */
  51.         break;
  52.     }
  53.     if (!rp) {            /* If not defined, allocate space for new  */
  54.     if ((rp = (struct macro *) malloc(sizeof(struct macro)))
  55.         == (struct macro *) 0)
  56.         fatal("No memory for macro");
  57.  
  58.     rp->m_next = macrohead;
  59.     macrohead = rp;
  60.     rp->m_flag = FALSE;
  61.  
  62.     if ((cp = malloc((unsigned) strlen(name) + 1)) == (char *) 0)
  63.         fatal("No memory for macro");
  64.     strcpy(cp, name);
  65.     rp->m_name = cp;
  66.     }
  67.     if ((cp = malloc((unsigned) strlen(val) + 1)) == (char *) 0)
  68.     fatal("No memory for macro");
  69.     strcpy(cp, val);        /* Copy in new value  */
  70.     rp->m_val = cp;
  71.  
  72.     return rp;
  73. }
  74.  
  75.  
  76. /*
  77.  *    Do the dirty work for expand
  78.  */
  79. void
  80. doexp(to, from, len, buf)
  81.     char          **to;
  82.     char           *from;
  83.     int            *len;
  84.     char           *buf;
  85. {
  86.     register char  *rp;
  87.     register char  *p;
  88.     register char  *q;
  89.     register struct macro *mp;
  90.  
  91.  
  92.     rp = from;
  93.     p = *to;
  94.     while (*rp) {
  95.     if (*rp != '$') {
  96.         *p++ = *rp++;
  97.         (*len)--;
  98.     } else {
  99.         q = buf;
  100.         if (*++rp == '{')
  101.         while (*++rp && *rp != '}')
  102.             *q++ = *rp;
  103.         else if (*rp == '(')
  104.         while (*++rp && *rp != ')')
  105.             *q++ = *rp;
  106.         else if (!*rp) {
  107.         *p++ = '$';
  108.         break;
  109.         } else
  110.         *q++ = *rp;
  111.         *q = '\0';
  112.         if (*rp)
  113.         rp++;
  114.         if (!(mp = getmp(buf)))
  115.         mp = setmacro(buf, "");
  116.         if (mp->m_flag)
  117.         fatal("Infinitely recursive macro %s", mp->m_name);
  118.         mp->m_flag = TRUE;
  119.         *to = p;
  120.         doexp(to, mp->m_val, len, buf);
  121.         p = *to;
  122.         mp->m_flag = FALSE;
  123.     }
  124.     if (*len <= 0)
  125.         error("Expanded line too line");
  126.     }
  127.     *p = '\0';
  128.     *to = p;
  129. }
  130.  
  131.  
  132. /*
  133.  *    Expand any macros in str.
  134.  */
  135. void
  136. expand(str)
  137.     char           *str;
  138. {
  139.     static char     a[LZ];
  140.     static char     b[LZ];
  141.     char           *p = str;
  142.     int             len = LZ - 1;
  143.  
  144.     strcpy(a, str);
  145.     doexp(&p, a, &len, b);
  146. }
  147.